home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’96
/
PredatorPrey
/
DialogAids.c
< prev
next >
Wrap
Text File
|
1996-06-22
|
16KB
|
683 lines
/* © 1988-91, Bowers Development Corp. */
/* DialogAids.c */
#include <Types.h>
#include <Quickdraw.h>
#include <Controls.h>
#include <Desk.h> /* for SystemTask */
#include <Dialogs.h>
#include <Events.h>
#include <Lists.h>
#include <Menus.h>
#include <TextEdit.h>
#include "EventLoop.h"
#include "Globals.h"
#include "ResourceDefs.h"
#include "WindowAids.h"
#include "Windowing.h"
#include "DoScrap.h"
#include "DialogAids.h"
#include <OSUtils.h>
#include <Packages.h>
#include <ToolUtils.h>
#include "Cursors.h" /* cursorRgn for WaitNextEvent */
#define gray 0xFFFFFFE8
#pragma segment DialogAids
/*----------*/
Rect GetDRect (short itemNr)
{
short itemType;
Handle itemHandle;
Rect itemRect;
GetDItem (qd.thePort, itemNr, &itemType, &itemHandle, &itemRect);
return (itemRect);
} /*GetDRect*/
/*----------*/
static Handle GetItemHandle (short itemNr);
static Handle GetItemHandle (short itemNr)
{
short itemType;
Handle itemHandle;
Rect itemRect;
GetDItem (qd.thePort, itemNr, &itemType, &itemHandle, &itemRect);
return (itemHandle);
} /*GetItemHandle*/
/*----------*/
pascal void LineItem (DialogPtr dialog,
short itemNr)
{
#pragma unused (dialog)
PenState savePen;
Rect itemRect;
GetPenState (&savePen);
PenNormal ();
/*PenPat (&qd.gray);*/
PenPat ((PatPtr)gray);
itemRect = GetDRect (itemNr);
MoveTo (itemRect.left, itemRect.top);
LineTo (itemRect.right - 1, itemRect.bottom - 1);
SetPenState (&savePen);
} /*LineItem*/
/*----------*/
pascal void RectItem (DialogPtr dialog,
short itemNr)
{
#pragma unused (dialog)
Rect itemRect;
PenState savePen;
GetPenState (&savePen);
PenNormal ();
itemRect = GetDRect (itemNr);
FrameRect (&itemRect);
SetPenState (&savePen);
} /*RectItem*/
/*----------*/
void SetUserItem (short itemNr,
ProcPtr doDraw)
{
short itemType;
Handle itemHandle;
Rect itemRect;
GetDItem (qd.thePort, itemNr, &itemType, &itemHandle, &itemRect);
SetDItem (qd.thePort, itemNr, itemType, (Handle) doDraw, &itemRect);
} /*SetUserItem*/
/*----------*/
void OutlineButton (short itemNr)
{
Rect itemRect;
PenState savePen;
GetPenState (&savePen);
PenNormal ();
PenSize (3, 3);
itemRect = GetDRect (itemNr);
InsetRect (&itemRect, -4, -4);
FrameRoundRect (&itemRect, 16, 16);
SetPenState (&savePen);
} /*OutlineButton*/
/*----------*/
void EnableDItem (short itemNr,
Boolean enable)
{
short itemType;
Handle itemHandle;
Rect itemRect;
ControlHandle control;
GetDItem (qd.thePort, itemNr, &itemType, &itemHandle, &itemRect);
if (enable) {
itemType &= ~itemDisable;
} else {
itemType |= itemDisable;
}
SetDItem (qd.thePort, itemNr, itemType, itemHandle, &itemRect);
if ((itemType & ctrlItem) != 0) { /* it's a control */
control = (ControlHandle) itemHandle;
HiliteScroll (control, enable);
} /*else … do nothing?*/
} /*EnableDItem*/
/*----------*/
void SetDText (short itemNr,
Str255 text)
{
Handle itemHandle;
itemHandle = GetItemHandle (itemNr);
SetIText (itemHandle, text);
} /*SetDText*/
/*----------*/
void GetDText (short itemNr,
Str255 text)
{
Handle itemHandle;
itemHandle = GetItemHandle (itemNr);
GetIText (itemHandle, text);
} /*GetDText*/
/*----------*/
void SetDNum (short itemNr,
long num)
{
Str255 text;
NumToString (num, text);
SetDText (itemNr, text);
} /*SetDNum*/
/*----------*/
void GetDNum (short itemNr,
long *num)
{
Str255 text;
GetDText (itemNr, text);
StringToNum (text, num);
} /*GetDNum*/
/*----------*/
void SetCheckbox (short itemNr,
Boolean checked)
{
ControlHandle control;
control = (ControlHandle) GetItemHandle (itemNr);
SetCtlMax (control, 1);
SetCtlValue (control, checked);
} /*SetCheckbox*/
/*----------*/
void DoCheckbox (short itemNr,
Boolean *checked)
{
*checked = !*checked;
SetCheckbox (itemNr, *checked);
} /*DoCheckbox*/
/*----------*/
void SetRadio (short firstItem,
short choice)
{
SetCheckbox (firstItem + (choice - 1), true);
} /*SetRadio*/
/*----------*/
void DoRadio (short firstItem,
short itemNr,
short *choice)
{
SetCheckbox (firstItem + (*choice - 1), false);
*choice = itemNr - firstItem + 1;
SetCheckbox (firstItem + (*choice - 1), true);
} /*DoRadio*/
/*----------*/
static void InvertIcon (short itemNr);
static void InvertIcon (short itemNr)
{
Rect itemRect;
itemRect = GetDRect (itemNr);
InsetRect (&itemRect, 1, 1);
InvertRect (&itemRect);
} /*InvertIcon*/
/*----------*/
void DoIconRadio (short firstIcon,
short itemNr,
short *choice)
{
InvertIcon (firstIcon + (*choice - 1));
*choice = (itemNr - firstIcon) + 1;
InvertIcon (firstIcon + (*choice - 1));
} /*DoIconRadio*/
/*----------*/
void SetCtlChoice (short itemNr,
short choice)
{
ControlHandle control;
control = (ControlHandle) GetItemHandle (itemNr);
SetCtlValue (control, choice);
} /*SetCtlChoice*/
/*----------*/
short GetCtlChoice (short itemNr)
{
ControlHandle control;
control = (ControlHandle) GetItemHandle (itemNr);
return (GetCtlValue (control));
} /*GetCtlChoice*/
/*----------*/
void DoPalette (short itemNr,
short *choice)
{
*choice = GetCtlChoice (itemNr);
} /*DoPalette*/
/*----------*/
void DoMultiState (short itemNr,
short *value)
{
ControlHandle control;
control = (ControlHandle) GetItemHandle (itemNr);
if (*value == GetCtlMax (control)) {
*value = GetCtlMin (control);
} else {
*value += 1;
}
SetCtlValue (control, *value);
} /*DoMultiState*/
/*----------*/
void SetScrollItem (short itemNr,
short value,
short min,
short max,
short pageSize)
{
ControlHandle scroll;
scroll = (ControlHandle) GetItemHandle (itemNr);
SetCtlMin (scroll, min);
SetCtlMax (scroll, max);
SetCtlValue (scroll, value);
SetCRefCon (scroll, pageSize);
HiliteScroll (scroll, (min < max));
} /*SetScrollItem*/
/*----------*/
void DoScrollItem (short itemNr,
short *value)
{
ControlHandle scroll;
scroll = (ControlHandle) GetItemHandle (itemNr);
*value = GetCtlValue (scroll);
} /*DoScrollItem*/
/*----------*/
void DrawPopup (short itemNr,
short menuID,
short choice)
{
Rect itemRect;
itemRect = GetDRect (itemNr);
UpdatePopup (itemRect, menuID, choice);
} /*DrawPopup*/
/*----------*/
void DoPopup (short itemNr,
short menuID,
short *choice)
{
Rect itemRect;
itemRect = GetDRect (itemNr);
TrackPopup (itemRect, menuID, choice);
} /*DoPopup*/
/*----------*/
void InvertLabel (short itemNr)
{
Rect itemRect;
itemRect = GetDRect (itemNr);
InvertRect (&itemRect);
} /*InvertLabel*/
/*----------*/
ListHandle Vert1List (short itemNr)
{
Rect itemRect;
ListHandle list;
itemRect = GetDRect (itemNr);
list = NewV1List (itemRect, qd.thePort);
return (list);
} /*Vert1List*/
/*----------*/
Boolean FilterList (EventRecord *event,
ListHandle list,
short listItem,
short dblClickItem,
short *itemHit)
{
Boolean filtered;
Point mousePos;
filtered = false;
if ((*event).what == mouseDown) {
mousePos = (*event).where;
GlobalToLocal (&mousePos);
if (FindDItem (qd.thePort, mousePos) + 1 == listItem) {
if (LClick (mousePos, (*event).modifiers, list)) {
*itemHit = dblClickItem;
} else {
*itemHit = listItem;
}
filtered = true;
}
}
return (filtered);
} /*FilterList*/
/*----------*/
Boolean FilterScroll (EventRecord *event,
short scrollItem,
ScrollProcPtr actionProc,
short *itemHit)
{
Boolean filtered;
Point mousePos;
short partCode;
ControlHandle whichControl;
filtered = false;
if ((*event).what == mouseDown) {
mousePos = (*event).where;
GlobalToLocal (&mousePos);
if (FindDItem (qd.thePort, mousePos) + 1 == scrollItem) {
partCode = FindControl (mousePos, qd.thePort, &whichControl);
if (partCode != 0) {
TrackScroll (whichControl, partCode, mousePos, actionProc);
*itemHit = scrollItem;
filtered = true;
}
}
}
return (filtered);
} /*FilterScroll*/
/*----------*/
void FilterIcon (EventRecord *event,
short firstIcon,
short choice)
{
short itemType;
Handle itemHandle;
Rect itemRect;
if (((*event).what == updateEvt)
&& ((WindowPtr) (*event).message == qd.thePort)) {
GetDItem (qd.thePort, firstIcon + (choice - 1), &itemType, &itemHandle, &itemRect);
PlotIcon (&itemRect, itemHandle);
ValidRect (&itemRect);
InsetRect (&itemRect, 1, 1);
InvertRect (&itemRect);
}
} /*FilterIcon*/
/*----------*/
pascal Boolean StandardFilter (DialogPtr whichDialog,
EventRecord *event,
short *itemHit)
{
#define returnKey 13
#define enter 3
#define esc 27
#define pushButton (ctrlItem + btnCtrl)
Boolean filtered;
DialogPeek whichPeek;
short defItem; /* aDefItem: default button item number */
char key;
short itemType;
Handle itemHandle;
Rect itemRect;
long finalTicks;
filtered = false;
whichPeek = (DialogPeek) whichDialog;
defItem = whichPeek->aDefItem;
switch ((*event).what) {
case keyDown:
case autoKey:
key = (*event).message & charCodeMask;
if ((key == returnKey) || (key == enter) || (key == esc)
|| ((key == '.') && (((*event).modifiers & cmdKey) != 0))) {
if ((key == returnKey) || (key == enter)) {
*itemHit = defItem;
} else {
*itemHit = cancel;
}
GetDItem (whichDialog, *itemHit, &itemType, &itemHandle, &itemRect);
if ((itemType & itemDisable) == 0) {
if ((itemType & (255 - itemDisable)) == pushButton) {
HiliteControl ((ControlHandle) itemHandle, inButton);
#ifdef fast
#else
Delay (8, &finalTicks);
#endif
HiliteControl ((ControlHandle) itemHandle, 0);
}
filtered = true;
} /*if*/
} else {
if ((((*event).modifiers & cmdKey) != 0)
&& ((key == 'x') || (key == 'c') || (key == 'v'))) {
*itemHit = (whichPeek)->editField + 1;
switch (key) {
case 'x':
DlgCut (whichDialog);
scrapDirty = (*itemHit > 0);
break;
case 'c':
DlgCopy (whichDialog);
scrapDirty = (*itemHit > 0);
*itemHit = 0;
break;
case 'v':
DlgPaste (whichDialog);
break;
} /*switch*/
if (*itemHit > 0) {
GetDItem (whichDialog, *itemHit, &itemType, &itemHandle, &itemRect);
if ((itemType & itemDisable) == 0) {
filtered = true;
}
}
if (!filtered) {
(*event).what = nullEvent;
}
}
}
break;
case updateEvt:
if ((WindowPtr) (*event).message == qd.thePort) {
GetDItem (whichDialog, defItem, &itemType, &itemHandle, &itemRect);
if ((itemType & (255 - itemDisable)) == pushButton) {
OutlineButton (defItem);
}
} else if (!IsDialogEvent (event)) {
curEvent = *event;
DoUpdate (curEvent); /* update other windows *//* EventLoop.c */
}
break;
} /*switch*/
return (filtered);
} /*StandardFilter*/
/*----------*/
/* This procedure is called by DoModalEvent to handle a mouseDown in a */
/* movable modal dialog. */
/*----------*/
static void DoMouseDown (void);
static void DoMouseDown (void)
{
long menuChoice;
short menuID;
short itemNr;
WindowPtr whichWindow;
short whichPart;
whichPart = FindWindow (curEvent.where, &whichWindow);
switch (whichPart) {
case inMenuBar:
menuChoice = MenuSelect (curEvent.where);
menuID = HiWord (menuChoice);
itemNr = LoWord (menuChoice);
if ((menuID != 0) && (itemNr != 0)) {
/*Need to handle Cut, Copy, Paste here just like StandardFilter does.*/
SysBeep (1);
} /* otherwise, MenuSelect returned 0. Either the user */
/* chose nothing, or the user chose from a System */
/* menu, and it's been taken care of */
break;
case inDrag:
if (whichWindow == FrontWindow ()) {
DoDrag (whichWindow); /*permit drag of this dialog*/
} else {
SysBeep (1); /*can't drag any other window*/
}
break;
case inSysWindow:
case inContent:
case inDesk:
case inGrow:
case inGoAway:
case inZoomIn:
case inZoomOut:
default:
SysBeep (1);
break;
} /*case*/
} /*DoMouseDown*/
/*----------*/
static Boolean sFiltered; /*used by DoModalEvent & MovableDialog*/
/*----------*/
static Boolean GetEvent (void);
static Boolean GetEvent ()
{
Boolean gotEvent;
if (sysConfig.hasWNE) {
gotEvent = WaitNextEvent (everyEvent, &curEvent, 0L, cursorRgn);
} else {
SystemTask ();
gotEvent = GetNextEvent (everyEvent, &curEvent);
}
return (gotEvent);
} /*GetEvent*/
/*----------*/
/* This function processes the next modal dialog event and then returns. It is called */
/* by MovableDialog, but it can also be called directly. Assumes that the frontmost */
/* window is a modal (or movable modal) dialog window. */
/* */
/* If the event is associated with an item in the current dialog, returns the item number. */
/* If filterProc (or System 7.0's standard filter, or DialogSelect) handled the event */
/* (and presumably set itemHit); sets sFiltered to true, otherwise sets sFiltered to false. */
/* If the event is not associated with an item in the current dialog, returns 0. */
/*----------*/
short DoModalEvent (ModalFilterProcPtr filterProc)
{
WindowPtr eventWindow;
short itemHit; /*function return value*/
GrafPtr savePort;
DialogPtr theDialog;
GetPort(&savePort);
sFiltered = false;
itemHit = 0;
theDialog = FrontWindow ();
if (GetEvent () || (curEvent.what == nullEvent)) {
/*got an event to process*/
if (curEvent.what == app4Evt) {
/* here so event won't get swallowed by IsDialogEvent */
/* and so suspend/resume can be translated to deactivate/activate */
DoApp4Event ();
}
if (IsDialogEvent (&curEvent)) {
/*it's a dialog event*/
if ((curEvent.what == activateEvt) || (curEvent.what == updateEvt)) {
eventWindow = (DialogPtr) curEvent.message;
} else {
eventWindow = FrontWindow ();
}
SetPort (eventWindow);
if (filterProc != NULL) {
/*caller passed a filterProc*/
sFiltered = (*filterProc) (theDialog, &curEvent, &itemHit);
} else {
/*filterProc == NULL*/
/* Need to do if (gestalt then if gestalt(stdfilter available)) sFiltered = stdfilter () */
/* See Tech Note #304 p. 4 for GetStdFilterProc */
sFiltered = StandardFilter (theDialog, &curEvent, &itemHit);
}
if (!sFiltered) {
/*either filterProc == NULL, or filterProc
didn't process curEvent (although filterProc
may have altered curEvent)*/
/*Don't need to handle Cmd-keys here, since StandardFilter does.*/
sFiltered = DialogSelect (&curEvent, &theDialog, &itemHit);
} /*otherwise, sFiltered*/
} else { /*it's not a dialog event*/
switch (curEvent.what) {
case mouseDown:
DoMouseDown ();
break;
case mouseUp:
case keyDown:
case autoKey:
/*do nothing*/
break;
case updateEvt:
DoUpdate (curEvent);
break;
case activateEvt:
/*do nothing, to avoid setting cur to noCur*/
break;
case diskEvt:
DoDiskEvent ();
break;
default:
/*Do nothing*/
break;
} /*case*/
} /*if dialog event or not*/
} /*otherwise, not GetEvent and curEvent.what != nullEvent*/
SetPort(savePort);
return (itemHit);
} /*DoModalEvent*/
/*----------*/
/* This procedure works just like ModalDialog, case plus: */
/* - clicking and dragging in the dialog's title bar will move the dialog */
/* - application windows update */
/* - under System 7.0, uses the same standard filter proc as ModalDialog */
/* Under System 7.0, the dialog window's procID should be movableDBoxProc */
/* (== 5, from Windows.p). */
/* Under System 6.x, procID movableDBoxProc is equivalent to dBoxProc, and the */
/* user won't be able to drag the dialog. However, if the procID is noGrowDocProc, */
/* then the user will be able to drag the dialog. */
/* Modifies global curEvent (Globals.p). */
/*----------*/
void MovableDialog (ModalFilterProcPtr filterProc,
short *itemHit)
{
/*Need to disable all of application's menus except Cut/Copy/Paste here*/
*itemHit = 0;
if (FrontWindow () != NULL) {
sFiltered = false;
while (!sFiltered) {
*itemHit = DoModalEvent (filterProc); /*also sets sFiltered*/
} /*while not sFiltered*/
} /*otherwise, no front window*/
} /*MovableDialog*/